package com.ruoyi.project.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.domain.vo.GoodsVo;
import com.ruoyi.project.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.service.ISysGoodsService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author theme
 * @date 2020-06-23
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService {
    @Autowired
    private SysGoodsMapper sysGoodsMapper;

    @Autowired
    private SysGoodsImgMapper sysGoodsImgMapper;

    @Autowired
    private SysGoodsSpecificationMapper sysGoodsSpecificationMapper;

    @Autowired
    private SysGoodsProductMapper sysGoodsProductMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    /**
     * 查询【请填写功能名称】
     *
     * @param goodsId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public SysGoods selectSysGoodsById(Long goodsId) {
        return sysGoodsMapper.selectSysGoodsById(goodsId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysGoods 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @DataScope(deptAlias = "a")
    @Override
    public List<SysGoods> selectSysGoodsList(SysGoods sysGoods) {
        List<SysGoods> goods = sysGoodsMapper.selectSysGoodsList(sysGoods);
        return goods;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysGoods(SysGoods sysGoods) {
        sysGoods.setCreateTime(DateUtils.getNowDate());
        return sysGoodsMapper.insertSysGoods(sysGoods);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysGoods(SysGoods sysGoods) {
        sysGoods.setUpdateTime(DateUtils.getNowDate());
        return sysGoodsMapper.updateSysGoods(sysGoods);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param goodsIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByIds(Long[] goodsIds) {
        return sysGoodsMapper.deleteSysGoodsByIds(goodsIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param goodsId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysGoodsById(Long goodsId) {
        return sysGoodsMapper.deleteSysGoodsById(goodsId);
    }

    @Override
    public AjaxResult create(GoodsVo goodsVo) {
        Long categoryId = goodsVo.getGoods().getCategoryId();
        if (categoryId == null) {
            return AjaxResult.error("未选择商品品牌商！");
        }
        if (goodsVo.getGoods().getType() == null) {
            return AjaxResult.error("未选择商品种类！");
        }
        SysGoods goods = goodsVo.getGoods();
        SysGoodsImg[] goodsImgs = goodsVo.getGoodsImgs();
        SysGoodsProduct[] goodsProducts = goodsVo.getGoodsProducts();
        SysGoodsSpecification[] goodsSpecifications = goodsVo.getGoodsSpecifications();

        SysGoods sysGoods = sysGoodsMapper.selectSysGoodsByName(goods.getName());
        if (sysGoods != null) {
            return AjaxResult.error("商品名已经存在，请重新填写！");
        }
        goods.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        goods.setDelFlag("0");
        sysGoodsMapper.insertSysGoods(goods);

        for (int j = 0; j < goodsImgs.length; j++) {
            goodsImgs[j].setGoodsId(goods.getGoodsId());
            sysGoodsImgMapper.insertSysGoodsImg(goodsImgs[j]);
        }
        for (SysGoodsProduct goodsProduct : goodsProducts) {
            goodsProduct.setGoodsId(goods.getGoodsId());
            goodsProduct.setDelFlag("0");
            sysGoodsProductMapper.insertSysGoodsProduct(goodsProduct);
        }
        for (SysGoodsSpecification goodsSpecification : goodsSpecifications) {
            goodsSpecification.setGoodsId(goods.getGoodsId());
            goodsSpecification.setDelFlag("0");
            sysGoodsSpecificationMapper.insertSysGoodsSpecification(goodsSpecification);
        }
        return AjaxResult.success("商品新增成功！");
    }

    @Override
    public AjaxResult update(GoodsVo goodsVo) {
        Long categoryId = goodsVo.getGoods().getCategoryId();
        if (categoryId == null) {
            return AjaxResult.error("未选择商品品牌商！");
        }
        if (goodsVo.getGoods().getType() == null) {
            return AjaxResult.error("未选择商品种类！");
        }
        SysGoods goods = goodsVo.getGoods();
        SysGoodsImg[] goodsImgs = goodsVo.getGoodsImgs();
        SysGoodsProduct[] goodsProducts = goodsVo.getGoodsProducts();
        SysGoodsSpecification[] goodsSpecifications = goodsVo.getGoodsSpecifications();

        if (sysGoodsMapper.updateSysGoods(goods) == 0) {
            return AjaxResult.error("商品信息更新失败！");
        }

        Long goodsId = goods.getGoodsId();

        sysGoodsImgMapper.deleteSysGoodsImgByGoodsId(goodsId);
        for (SysGoodsImg goodsImg : goodsImgs) {
            goodsImg.setGoodsId(goodsId);
            sysGoodsImgMapper.insertSysGoodsImg(goodsImg);
        }

        SysGoodsProduct product = new SysGoodsProduct();
        product.setGoodsId(goodsId);
        product.setDelFlag("1");
        sysGoodsProductMapper.updateSysGoodsProductGoodsId(product);
        for (SysGoodsProduct goodsProduct : goodsProducts) {
            goodsProduct.setGoodsId(goodsId);
            sysGoodsProductMapper.insertSysGoodsProduct(goodsProduct);
        }

        SysGoodsSpecification specification = new SysGoodsSpecification();
        specification.setGoodsId(goodsId);
        specification.setDelFlag("1");
        sysGoodsSpecificationMapper.updateSysGoodsSpecification(specification);
        for (SysGoodsSpecification goodsSpecification : goodsSpecifications) {
            goodsSpecification.setGoodsId(goodsId);
            sysGoodsSpecificationMapper.insertSysGoodsSpecification(goodsSpecification);
        }
        return AjaxResult.success("商品信息更新成功！");
    }

    @Override
    public AjaxResult queryOne(Long goodsId) {
        SysGoods goods = sysGoodsMapper.selectSysGoodsById(goodsId);
        List<SysGoodsImg> sysGoodsImgs = sysGoodsImgMapper.selectSysGoodsImgByGoodsId(goodsId);
        List<SysGoodsSpecification> sysGoodsSpecifications = sysGoodsSpecificationMapper.selectSysGoodsSpecificationByGoodsId(goodsId);
        List<SysGoodsProduct> sysGoodsProducts = sysGoodsProductMapper.selectSysGoodsProductByGoodsId(goodsId);

        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        map.put("sysGoodsImgs", sysGoodsImgs);
        map.put("sysGoodsSpecifications", sysGoodsSpecifications);
        map.put("sysGoodsProducts", sysGoodsProducts);

        return AjaxResult.success(map);
    }
}
